package com.sunny.ynhz.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Base64;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 文件操作类 Created by Nereo on 2015/4/8.
 */
public class FileUtils {
    public static File createTmpJsonFile(Context context) {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "SelectImg"
                    + File.separator + "cache";
            File file = new File(absolutePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(file, fileName + ".json");
            return tmpFile;
        } else {
            File cacheDir = context.getCacheDir();
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(cacheDir, fileName + ".json");
            return tmpFile;
        }

    }

    public static File createFile(Context context, String imgType) {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "SelectImg"
                    + File.separator + "cache";
            File file = new File(absolutePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(file, fileName + "." + imgType);
            return tmpFile;
        } else {
            File cacheDir = context.getCacheDir();
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(cacheDir, fileName + "." + imgType);
            return tmpFile;
        }

    }

    public static File createTmpFile(Context context) {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "SelectImg"
                    + File.separator + "cache";
            File file = new File(absolutePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(file, fileName + ".jpg");
            return tmpFile;
        } else {
            File cacheDir = context.getCacheDir();
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(cacheDir, fileName + ".jpg");
            return tmpFile;
        }

    }

    public static String encodeBase64File(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer, Base64.DEFAULT);
    }


    public static File createTmpVideo(Context context, String dir) {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "Video"
                    + File.separator + "cache";
            File file = new File(absolutePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File tmpFile = new File(file, dir);
            return tmpFile;
        } else {
            File cacheDir = context.getCacheDir();
            File tmpFile = new File(cacheDir, dir);
            return tmpFile;
        }

    }

    /**
     * 取得后缀名
     *
     * @param filename
     * @return
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    public static String markFilePathString(Context context) {
        String filePathString = "";
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "apk"
                    + File.separator + "cache";
            filePathString = absolutePath;
        } else {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "apk" + timeStamp;
            filePathString = fileName;
        }
        return filePathString;

    }

    public static String markDownFilePathString(Context context) {
        String filePathString = "";
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
            File pic = Environment.getExternalStorageDirectory();
            String absolutePath = pic.getPath() + File.separator + "apk"
                    + File.separator + "ynhz";
            filePathString = absolutePath;
        } else {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.CHINA).format(new Date());
            String fileName = "apk" + timeStamp;
            filePathString = fileName;
        }
        return filePathString;

    }

    /**
     * 将String数据存为文件
     */
    public static File getFileFromBytes(String name, String path) {
        byte[] b = name.getBytes();
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(path);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }

    public static String saveBitmapFile(Bitmap bitmap, Context context) {
        File file = createFile(context, "png");
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
}
